package chapter.fifth.item29;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class Favorites {
	private Map<Class<?>,Object> favorites =
			new HashMap<Class<?>,Object>();
	public <T> void putFavorite(Class<T> type,T instance){
		if(type==null)
			throw new IllegalArgumentException("type is null");
		favorites.put(type, type.cast(instance));
	}
	
	public Map<Class<?>, Object> getFavorites() {
		return favorites;
	}

	public <T> T getFavorite(Class<T> type){
		return type.cast(favorites.get(type));
	}
	
	public static void main(String[] args) {
		Favorites f = new Favorites();
		f.putFavorite(String.class,"hello");
		f.putFavorite(Integer.class, 1);
		f.putFavorite(Character.class, 'a');
		List<String> l = new ArrayList<String>();
		l.add("hi");
		f.putFavorite(List.class, l);
		System.out.println(JSON.toJSONString(f));
	}
}
